home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 6220 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.7 KB  |  93 lines

  1. Newsgroups: comp.lang.c++
  2. Path: nntp.coast.net!torn!nott!emr1!jagrant
  3. From: jagrant@emr1.emr.ca (John Grant)
  4. Subject: design problem
  5. Message-ID: <DMMwLp.Awv@emr1.emr.ca>
  6. Organization: Energy, Mines, and Resources, Ottawa
  7. References: <4flp0o$101@lal.interserv.net>
  8. Date: Sun, 11 Feb 1996 23:15:25 GMT
  9.  
  10. I'm good at C, struggling at C++: I know the concepts (I can use existing
  11. classes), but I'll be damned if I can design/write my own.
  12.  
  13. Problem
  14. -------
  15. I have several file types: A,B,C...
  16. Each one has an Open(), Close(), Draw() & Size() function, the nature
  17. of which is specific to the file type, but which is common to all file types.
  18. Each file type has specific functions as well, i.e. GetAStuff() for 'A' files.
  19.  
  20. Implementation
  21. --------------
  22. I want to create a class I can use for each file, regardless of type. Later,
  23. I will want to put a bunch of these into a container, i.e. an array of some
  24. sort:
  25.     cMYFILE *f1=new cMYFILE("xxx.a");    //file type 'A'
  26.     cMYFILE *f2=new cMYFILE("yyy.b");    //file type 'B'
  27.     ...
  28.  
  29. I need to call the 'generic' functions which are common to all file types:
  30.     f1->Open();
  31.     f1->Draw();
  32.     f1->Size();
  33.  
  34.     f2->Open();
  35.     f2->Draw();
  36.     f2->Size();
  37.     
  38. However, I also need to be able to call functions which are specific to the
  39. file type. Presumably, I must first inspect the object to determine the
  40. file type:
  41.     if(f1->Type()=='a') f1->GetAStuff();
  42.     if(f1->Type()=='b') f1->GetBStuff();
  43.  
  44. I haven't got a clue how to design these classes or how many I need. I started
  45. out with a base class:
  46.     class cMYFILE{
  47.       public: cMYFILE(char *name);
  48.           ~cMYFILE();
  49.           virtual int Open(void)=0;
  50.           virtual int Draw(void)=0;
  51.           virtual int Size(void)=0;
  52.           virtual char Type(void)=0;
  53.     };
  54.  
  55. and then decided I needed to derive an A,B,C class from cMYFILE in order
  56. to implement the generic virtual functions and add in other file-specific
  57. functions:
  58.     class cMYFILE_A:public cMYFILE{
  59.       public:  int Open(void);
  60.            int Draw(void);
  61.            int Size(void);
  62.            int Type(void);
  63.            int GetAStuff(void);
  64.     };
  65.  
  66.     class cMYFILE_B:public cMYFILE{
  67.       public:  int Open(void);
  68.            int Draw(void);
  69.            int Size(void);
  70.            int Type(void);
  71.            int GetBStuff(void);
  72.     };
  73.  
  74. Then I got confused.  I don't know which functions should be virtual,
  75. which functions should be pure virtual and what stuff should be in the
  76. base class and what stuff should be in the derived classes.  I'm just
  77. confused.
  78.  
  79. A related problem, which I haven't dealt with is to create a container
  80. class which will hold a list of these files, so I can do:
  81.     nfile=filelist->nfile();
  82.     for(i=0;i<nfile;i++){
  83.       filelist->Open(i);
  84.       filelist->Draw(i);
  85.       filelist->Close(i);
  86.     }
  87.  
  88. Thanks for helping.
  89. -- 
  90. John A. Grant                        jagrant@emr1.emr.ca
  91. Airborne Geophysics
  92. Geological Survey of Canada, Ottawa
  93.